package com.myblog.controller;

import com.myblog.domain.GlobalResponse;
import com.myblog.domain.Vo.ArticleVo;
import com.myblog.domain.dto.AddArticleDto;

import com.myblog.domain.entity.ArticleTag;
import com.myblog.domain.entity.enums.GlobalResponseEnum;
import com.myblog.services.ArticleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/content/article")
public class ArticleController {


    @Resource
    ArticleService articleService;

    @PostMapping()
    public GlobalResponse<?> NewArticle(@RequestBody AddArticleDto article){
        return articleService.newArticle(article);
    }

    @GetMapping("/list")
    public GlobalResponse<List<ArticleVo>> getArticleList(Integer pageNum, Integer pageSize, String title, String summary){
        return  articleService.getArticleList(pageNum,pageSize,title,summary);
    }

    @GetMapping("/{id}")
    public GlobalResponse<List<ArticleTag>> getArticleById(@PathVariable Long id){
        return articleService.AdminGetArticle(id);
    }

    @PutMapping()
    public GlobalResponse<?> updateArticle(@RequestBody AddArticleDto articleDto){return  articleService.updateArticle(articleDto);}

    @DeleteMapping("/{id}")
    public GlobalResponse<?> deleteArticleById(@PathVariable String id){
        if(articleService.removeById(id)){
            return GlobalResponse.ok();
        }
        return GlobalResponse.error(GlobalResponseEnum.ID_NO_EXIST);
    }

}
